import Vue from 'vue'
import Router from 'vue-router'
// 2021年1月25日之前的路由在index_copy.js文件中

Vue.use(Router)

const router = new Router({
  mode: 'history',
  base: `${process.env.VUE_APP_BASE_URL_PC}${process.env.VUE_APP_VERSION_PC}/`,
  routes: [
    {
      path: '/',
      redirect: '/login'
    },
    {
      path: '/login',
      name: 'Login',
      component: () => import('views/Login'),
      meta: { title: '登录-MedChat' }
    },
    {
      path: '/reset',
      name: 'Reset',
      component: () => import('views/Login/reset'),
      meta: { title: '重置密码-MedChat' }
    },
    {
      path: '/system',
      name: 'selectPlatform',
      component: () => import('views/selectPlatform'),
      meta: { title: '选择平台-MedChat' },
      redirect: '/system/platformList',
      children: [
        {
          path: '/system/platformList',
          name: 'platformList',
          component: () => import('views/selectPlatform/platformList'),
          meta: {title: 'WeChat账号'}
        },
        {
          path: '/system/tenantManage',
          name: 'tenantManage',
          component: () => import('views/selectPlatform/tenantManage/index'),
          meta: {title: '租户管理'}
        },
        {
          path: '/system/manage',
          name: 'systemManage',
          component: () => import('views/selectPlatform/system'),
          meta: {title: '系统运维'}
        },
        {
          path: '/system/app',
          name: 'systemApp',
          component: () => import('views/selectPlatform/app'),
          meta: {title: '系统应用'}
        },
        {
          path: '/system/accountInfo',
          name: 'accountInfo',
          component: () => import('views/selectPlatform/accountInfo'),
          meta: {title: '账号信息'}
        }
      ]
    },
    {
      path: '/home',
      name: 'Home',
      component: () => import('views/Home'),
      redirect: '/home/homepage',
      children: [
        {
          path: '/home/homepage',
          name: 'Homepage',
          component: () => import('views/Homepage'),
          meta: {
            title: '首页-MedChat',
            isOccupyAll: true
          }
        },
        // 公众号
        {
          path: '/home/weixin',
          name: 'weixin',
          component: () => import('views/weixin'),
          redirect: '/home/weixin/mp/safe',
          children: [
            {
              path: '/home/weixin/mp/safe',
              name: 'safe',
              component: () => import('views/Subscription/safe'),
              meta: { title: '安全设置-MedChat' }
            }
          ]
        },
        // 企业号
        {
          path: '/home/cp',
          name: 'Enterprise',
          component: () => import('views/enterprise'),
          redirect: '/home/cp/manage/auth',
          children: [
            // 企业号管理相关
            {
              path: '/home/cp/manage/auth',
              name: 'cpAuth',
              component: () => import('views/enterprise/basic'),
              meta: { title: '企业号授权-MedChat' }
            }
          ]
        }
      ]
    },
    {
      path: '*',
      name: 'Notfound',
      component: () => import('views/Notfound'),
      meta: { title: '未知' }
    }
  ]
})

router.beforeEach((to, from, next) => {
  // 如果没有登录, 则只能回到登录页面
  if (to.name !== 'Login' && to.name !== 'Reset' && !sessionStorage.getItem('hasLogin')) {
    return next({name: 'Login'})
  }
  if (window.__axiosPromiseArr) {
    window.__axiosPromiseArr.forEach((ele, ind) => {
      ele.cancel()
      delete window.__axiosPromiseArr[ind]
    })
  }
  if (to.meta.title) {
    document.title = to.meta.title
  }
  // next()
  let limits = sessionStorage.getItem('menuList') ? JSON.parse(sessionStorage.getItem('menuList')) : []
  let pubLimits = [
    '/login',
    '/home/homepage',
    '/registered',
    '/reset',
    '/Notfound',
    '/home/addqa',
    '/medchat/newArticle',
    '/home/suggestion/ask',
    '/home/qarecord',
    '/home/qadetail',
    '/medchat/articals',
    '/medchat/artical',
    '/home/msgcf',
    '/home/msgcs',
    '/home/system'
  ]
  if (limits.filter(limit => to.path.indexOf(limit))[0] || pubLimits.filter(pubLimit => to.path.indexOf(pubLimit))[0]) {
    next()
  } else {
    next({path: '/Notfound'})
  }
})

export default router
